package top.iridescent.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherUtils {


    /**
     * 礼用正则表达式查找特定字符
     * @param str
     * @param p
     * @return
     */
    public static List<String> find(String str, String p) {
        List<String> list = new ArrayList<>();
        Matcher matcher = Pattern.compile(p).matcher(str);
        while (matcher.find()) {
            list.add(matcher.group().substring(2));
        }
        return list;
    }

    /**
     * 等比切割,取前n份
     * @param str
     * @param p  example : ".{1,4000}"
     * @param num
     * @return
     */
    public List<String> split(String str, String p, int num) {
        Matcher m = Pattern.compile(p).matcher(str);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(m.find() ? str.substring(m.start(), m.end()) : "");
        }
        return list;
    }

}
